package visiopuce.ui;

import java.util.Date;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import visiopuce.objets.Personne;
import visiopuce.objets.Prelevement;
import visiopuce.service.PersonneServiceImpl;
import visiopuce.service.PrelevementServiceImpl;

public class NouveauPrelevementDialog extends Dialog {
	private Text tNom;
	private Text tPrenom;
	private Text tNumero;
	private Button btnAssociAuDossier;
	private Button btnDrivDuPrlvement;
	private Personne personneAssociee;
	private DateTime dateNaissance;
	private Prelevement prelevementAssociee;
	private KeyStroke ks = null;
	private ContentProposalAdapter adapterNom;
	private ContentProposalAdapter adapterPrenom;
	private ContentProposalAdapter adapterNumero;
	
	public NouveauPrelevementDialog(Shell parentShell) {
		super(parentShell);
		try {
			ks = KeyStroke.getInstance("Ctrl+Space");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Créer un prélèvement");
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		btnAssociAuDossier = new Button(container, SWT.RADIO);
		btnAssociAuDossier.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnAssociAuDossier.setText("Associ\u00E9 au dossier :");
		new Label(container, SWT.NONE);

		Label lblParNom = new Label(container, SWT.NONE);
		lblParNom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParNom.setText("Nom (Ctrl+espace pour auto compl\u00E9ter):");

		tNom = new Text(container, SWT.BORDER);
		tNom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scp = new SimpleContentProposalProvider(null);
		adapterNom = new ContentProposalAdapter(tNom, new TextContentAdapter(), scp, ks, null);
		adapterNom.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tNom.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionNom(tNom.getText());
				}
			}

		});
		Label lblPrnom = new Label(container, SWT.NONE);
		lblPrnom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPrnom.setText("Pr\u00E9nom (Ctrl+espace pour auto compl\u00E9ter):");

		tPrenom = new Text(container, SWT.BORDER);
		tPrenom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scpPrenom = new SimpleContentProposalProvider(null);
		adapterPrenom = new ContentProposalAdapter(tPrenom, new TextContentAdapter(), scpPrenom, ks, null);
		adapterPrenom.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tPrenom.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionPrenom(tPrenom.getText());
				}
			}
		});

		Label lblParDateDe = new Label(container, SWT.NONE);
		lblParDateDe.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParDateDe.setText("Par date de naissance :");

		dateNaissance = new DateTime(container, SWT.BORDER);

		btnDrivDuPrlvement = new Button(container, SWT.RADIO);
		btnDrivDuPrlvement.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnDrivDuPrlvement.setText("D\u00E9riv\u00E9 du pr\u00E9l\u00E8vement :");
		new Label(container, SWT.NONE);

		Label lblParNumeroPrelevement = new Label(container, SWT.NONE);
		GridData gd_lblParNumeroPrelevement = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_lblParNumeroPrelevement.horizontalIndent = 1;
		lblParNumeroPrelevement.setLayoutData(gd_lblParNumeroPrelevement);
		lblParNumeroPrelevement.setText("Num\u00E9ro de pr\u00E9l\u00E8vement d'origine:");

		tNumero = new Text(container, SWT.BORDER);
		tNumero.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scpNum = new SimpleContentProposalProvider(null);
		adapterNumero = new ContentProposalAdapter(tNumero, new TextContentAdapter(), scpNum, ks, null);
		adapterNumero.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tNumero.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionNumero(tNumero.getText());
				}
			}

		});
		return container;
	}

	private void setAutoCompletionNumero(Text texte, String txt) {
		try {
			ContentProposalAdapter adapter = null;
			String[] defaultProposals = PrelevementServiceImpl.getInstance().getNumeroFromNumero(txt);
			SimpleContentProposalProvider scp = new SimpleContentProposalProvider(defaultProposals);
			scp.setProposals(defaultProposals);
			KeyStroke ks = KeyStroke.getInstance("Ctrl+Space");
			adapter = new ContentProposalAdapter(texte, new TextContentAdapter(), scp, ks, null);
			adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionNom(Text text, String txt) {
		try {
			ContentProposalAdapter adapter = null;
			String[] defaultProposals = PersonneServiceImpl.getInstance().getNomFromString(txt);
			SimpleContentProposalProvider scp = new SimpleContentProposalProvider(defaultProposals);
			scp.setProposals(defaultProposals);
			KeyStroke ks = KeyStroke.getInstance("Ctrl+Space");
			adapter = new ContentProposalAdapter(text, new TextContentAdapter(), scp, ks, null);
			adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionPrenom(Text text, String txt) {
		try {
			ContentProposalAdapter adapter = null;
			String[] defaultProposals = PersonneServiceImpl.getInstance().getPrenomFromNomPrenom(tNom.getText(), txt);
			SimpleContentProposalProvider scp = new SimpleContentProposalProvider(defaultProposals);
			scp.setProposals(defaultProposals);
			KeyStroke ks = KeyStroke.getInstance("Ctrl+Space");
			adapter = new ContentProposalAdapter(text, new TextContentAdapter(), scp, ks, null);
			adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// override method to use "Login" as label for the OK button
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Créer", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(576, 516);
	}

	@Override
	protected void okPressed() {
		prelevementAssociee = null;
		personneAssociee = null;
		if (btnAssociAuDossier.getSelection()) {
			if (tNom.getText() != null && tPrenom.getText() != null) {
				personneAssociee = PersonneServiceImpl.getInstance().getPersonneByNomPrenomDateNaissance(tNom.getText(), tPrenom.getText(), new Date(dateNaissance.getYear(), dateNaissance.getMonth(), dateNaissance.getDay()));
			} else {
				personneAssociee = new Personne();
				personneAssociee.setIdPersonne(0);
				personneAssociee.setNom(tNom.getText());
				personneAssociee.setPrenom(tPrenom.getText());
			}
		}
		if (btnDrivDuPrlvement.getSelection()) {
			prelevementAssociee = PrelevementServiceImpl.getInstance().getPrelevementByNumero(tNumero.getText());
		}

		super.okPressed();
	}

	public Personne getPersonneAssociee() {
		return personneAssociee;
	}

	public Prelevement getPrelevementAssociee() {
		return prelevementAssociee;
	}
	
	private void setAutoCompletionNumero(String txt) {
		try {
			String[] defaultProposals = PrelevementServiceImpl.getInstance().getNumeroFromNumero(txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterNumero.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionNom(String txt) {
		try {
			String[] defaultProposals = PersonneServiceImpl.getInstance().getNomFromString(txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterNom.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionPrenom(String txt) {
		try {
			String[] defaultProposals = PersonneServiceImpl.getInstance().getPrenomFromNomPrenom(tNom.getText(), txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterPrenom.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
